<?php

namespace App\Criteria;

use Prettus\Repository\Contracts\CriteriaInterface;
use Prettus\Repository\Contracts\RepositoryInterface;
use Zend\Diactoros\Request;

/**
 * Class RolesCriteria
 * @package namespace App\Criteria;
 */
class RolesCriteria implements CriteriaInterface
{
    /**
     * 按照角色筛选
     *
     * @param                     $model
     * @param RepositoryInterface $repository
     *
     * @return mixed
     */
    public function apply($model, RepositoryInterface $repository)
    {
        $roles = request('roles',null);

        if($roles){
            $model =  $model::whereHas('roles', function ($query) {
                $query->where('id', request('roles'));
            });
        }
        return $model;
    }
}
